from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from basic.basic_driver import DRIVER
from appium.webdriver.common.appiumby import AppiumBy
from loguru import logger

class BasicPage:
    def __init__(self):
        self.driver = DRIVER
    def _locator(self,locator, wait=None,freq=0.5):
        """
        定位元素
        :param locator: 元素定位器
        :param wait: 等待时间
        :param freq: 等待频率
        :return: 元素对象
        """
        return self.driver.find_element(*locator) if wait is None else \
            WebDriverWait(self.driver, wait, freq).until(EC.element_to_be_clickable(locator))
    def click(self, value, wait=None,freq=0.5):
        """
        点击元素
        :param value: 元素定位器
        :param wait: 等待时间
        :param freq: 等待频率
        :return: None
        """
        if 'UiSelector()' in value:
            locator = AppiumBy.ANDROID_UIAUTOMATOR,value
        else:
            locator = AppiumBy.XPATH,value
        logger.info(f"点击元素locator: {locator}")
        self._locator(locator, wait, freq).click()
